package com.mes.web.controller.biz;

import com.github.pagehelper.PageInfo;
import com.mes.biz.domain.BizOrderProcessRecord;
import com.mes.biz.domain.BizOrderProduct;
import com.mes.biz.domain.BizProcessUser;
import com.mes.biz.domain.BizProduct;
import com.mes.biz.domain.vo.ProcessRecordWithOrder;
import com.mes.biz.service.BizOrderProcessRecordService;
import com.mes.biz.service.BizOrderService;
import com.mes.biz.service.BizProcessUserService;
import com.mes.common.annotation.Log;
import com.mes.common.core.controller.BaseController;
import com.mes.common.core.domain.AjaxResult;
import com.mes.common.core.domain.R;
import com.mes.common.core.domain.model.LoginUser;
import com.mes.common.core.page.PageDomain;
import com.mes.common.enums.BusinessType;
import com.mes.common.exception.BizException;
import com.mes.common.utils.poi.ExcelUtil;
import com.mes.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 工序记录 Controller
 * @author hwg
 * @email hwg
 * @date 2023-05-15
 */
@RestController
@RequestMapping("/biz/bizOrderProcessRecord")
public class BizOrderProcessRecordController extends BaseController {

    @Autowired
    private BizOrderProcessRecordService bizOrderProcessRecordService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private BizProcessUserService bizProcessUserService;
    @Autowired
    private BizOrderService bizOrderService;

    /**
     * 查询工序记录列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrderProcessRecord:list')")
    @GetMapping("/list")
    public R<PageInfo> list(BizOrderProcessRecord bizOrderProcessRecord, HttpServletRequest request, HttpServletResponse response) {
        bizOrderProcessRecord.setPage(new PageDomain(request, response));
        return R.data(bizOrderProcessRecordService.findPage(bizOrderProcessRecord));
    }

    @GetMapping("/appletList")
    public AjaxResult appletList(BizOrderProcessRecord bizOrderProcessRecord, HttpServletRequest request, HttpServletResponse response) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        return AjaxResult.success(bizOrderProcessRecordService.appletList(bizOrderProcessRecord,loginUser.getUserId()));
    }


    /**
     * 修改工序记录
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrderProcessRecord:edit')")
    @Log(title = "修改工序记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated BizOrderProcessRecord bizOrderProcessRecord) {
        return R.status(bizOrderProcessRecordService.updateBizOrderProcessRecord(bizOrderProcessRecord));
    }



    /**
     * 内部小程序-新增工序完成记录
     */
    @Log(title = "完成记录工序", businessType = BusinessType.UPDATE)
    @PostMapping("/addRecord")
    public R addRecord(@RequestBody BizOrderProcessRecord bizOrderProcessRecord,HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        return R.status(bizOrderProcessRecordService.addRecord(bizOrderProcessRecord,loginUser.getUserId()));
    }

//    @GetMapping("/pieceProductionList")
//    public R<PageInfo> pieceProductionList(BizOrderProcessRecord bizOrderProcessRecord, HttpServletRequest request, HttpServletResponse response) {
//        bizOrderProcessRecord.setPage(new PageDomain(request, response));
//        PageInfo<ProcessRecordWithOrder> pageInfo = bizOrderProcessRecordService.pieceProductionList(bizOrderProcessRecord);
//        for (ProcessRecordWithOrder recordWithOrder : pageInfo.getList()) {
//            if (recordWithOrder.getUnitSalary() != null && recordWithOrder.getProcessVol() !=null){
//                recordWithOrder.setSalary(recordWithOrder.getUnitSalary().multiply(recordWithOrder.getProcessVol()));
//            }
//        }
//
//        return R.data(pageInfo);
//    }


//
//    @Log(title = "生产计件", businessType = BusinessType.EXPORT)
//    @GetMapping("/export")
//    public void export(BizOrderProcessRecord bizOrderProcessRecord,HttpServletResponse response) {
//        List<ProcessRecordWithOrder> list = bizOrderProcessRecordService.exportList(bizOrderProcessRecord);
//        ExcelUtil<ProcessRecordWithOrder> util = new ExcelUtil<ProcessRecordWithOrder>(ProcessRecordWithOrder.class);
//        util.printExcel(response,list, "产品数据");
//    }
}
